package com.cskaoyan.controller.wx;


import com.cskaoyan.bean.entity.Category;
import com.cskaoyan.bean.entity.Goods;
import com.cskaoyan.bean.entity.Keyword;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.service.CategoryService;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 *  小程序商品Controller
 *
 */
@RestController
@RequestMapping("wx/goods")
public class WxGoodsController {

    @Resource
    GoodsService goodsService;


    @Autowired
    UserService userService;

    @Autowired
    CategoryService categoryService;


    /**
     * 商品详情
     *
     * @param id
     * @return
     */
    @GetMapping("detail")
    public BaseRespVo getWxDetail(Integer id) {
        // 获取用户id
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getSession().getAttribute("username");
        Integer userId = userService.getUserId(username);
        WxGoodsDetailVo goodsDetailVo = goodsService.getWxDetail(id, userId);
        return BaseRespVo.ok(goodsDetailVo);
    }

    /**
     *  获取商品对应的类目
     *
     */
    @RequestMapping("category")
    public BaseRespVo category(Integer id){
        WxGetCategoryInfoAccordingToGoodsCategoryVo categoryVo = categoryService.getCategoryInfo(id);
        return BaseRespVo.ok(categoryVo);
    }

    /**
     * @param categoryId
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(Integer categoryId,Integer brandId,String keyword, BaseParam baseParam) {

        // 根据二级类目获取二级类目下的商品
        List<Goods> wxGoodsListVo = categoryService.getGoodsInfo(categoryId,brandId,keyword,baseParam);

        // 获取二级类目信息
        List<Category> categoryList = categoryService.getAllSecondCategory(categoryId,brandId,keyword,baseParam);
        return BaseRespVo.ok(WxGoodsAndCategoryListPageData.ok(wxGoodsListVo,categoryList));
    }

    /**
     *  商品相关联的商品信息
     *  根据商品id获取当前类目下的相似商品
     *
     */
    @RequestMapping("related")
    public BaseRespVo related(Integer id){
        List<Goods> goodsList = goodsService.getRelated(id);
        return BaseRespVo.ok(BasePageData.ok(goodsList));
    }

    /**
     *  获取商品的数量
     *  获取所有类目下的商品个数
     */
    @RequestMapping("count")
    public BaseRespVo count(){
        int goodsNum = goodsService.getAllGoodsNum();
        return BaseRespVo.ok(goodsNum);
    }


}
